<script>
	export default {
		globalData: {
			code: '',
			ser_url: 'https://dmapi.doit10019.com/api/',
			// ser_url: 'http://192.168.1.101:3000/',
			userId: ''
		},
		onLaunch: function(e) {
			console.log(9966)
		},
		onShow: function(e) {
			const params = {};
			const urlss =window.location.href;
			const str = decodeURIComponent(window.location.href);
			const url = str.substring(0, str.length - 2);
			// const url = 'http://xmswjjh.doit10019.com/h5test?open_id=askdaqweaqewqe&token=1231231231';
			const paramStr = url.split('?')[1];
			if (paramStr) {
				const paramArr = paramStr.split('&');
				for (let i = 0; i < paramArr.length; i++) {
					const param = paramArr[i].split('=');
					const key = decodeURIComponent(param[0]);
					const value = decodeURIComponent(param[1]);
					params[key] = value;
				}
			}
			let token = params.token
			let open_id = params.open_id
			// let valuesd = '1'
			// let open_id = str.substring(0, str.length - 2);
			uni.setStorageSync('OPENID',open_id)
			uni.setStorageSync('token', token);
			// uni.setStorageSync('valuesd', valuesd);
			console.log(uni.getStorageSync('token'))
			console.log(uni.getStorageSync('OPENID'))
			const code = ''
			if (uni.getStorageSync('token')) {
				this.infoId()
			}
			if (!uni.getStorageSync('token')) {
				// #ifdef H5
				let params = this.getRequestParams();
				let code = params["code"];
				// #endif
					
				//let code = e.query.code;
				const openid = uni.getStorageSync('OPENID');
				let redirect_uri = window.location.href;
				console.log('redirect_uri=',redirect_uri);
				// const scope = 'snsapi_base';
				const scope = 'snsapi_userinfo';
				//不存在code
				if((!code || code == undefined) && !openid){
					//获取code （为了方便展示参数 此处缩紧了，真实情况最好不要缩紧）
					document.location.replace(
					`https://open.weixin.qq.com/connect/oauth2/authorize?
						appid=wx77038186d1e9dcb6
						&redirect_uri=${encodeURIComponent(redirect_uri)}
						&response_type=code
						&scope=${scope}
						&state=STATE#wechat_redirect`);
				}
				
				if((code && code != undefined) && !openid){
					// 请求后端接口获取用户的信息
					this.getOpenId(code);
					//console.log('result=',result);
					
				}
			}
		},
		onHide: function() {
			console.log('App Hide')
		},
		watch: {
			$route() {
				this.directRightUrl()
			}
		},
		methods:{
			infoId(){
				uni.request({
					url: this.globalData.ser_url + "frontend/v1/user/info",
					method: 'POST',
					data: {},
					header: {
						"Content-Type": "application/x-www-form-urlencoded",
						'Authorization': 'Bearer ' + uni.getStorageSync('token'),
						
					},
					success: function(res) {
						let result = res.data;
						// 保存用户的openid
						if (result.data.user) {
							uni.setStorageSync('userlist',result.data.user)
							uni.setStorageSync('OPENID',result.data.user.wxopenid);
						}
					}
				})
			},
			getOpenId(code){
				uni.request({
					url: this.globalData.ser_url + "frontend/v1/wechat/official/getOpenid",
					method: 'GET',
					data: {
						code: code,
					},
					header: {
						"Content-Type": "application/x-www-form-urlencoded"
					},
					success: function(res) {
						let result = res.data;
						// 保存用户的openid
						uni.setStorageSync('OPENID',result.data.open_id);
						if (result.data.user) {
							uni.setStorageSync('token',result.data.access_token)
							uni.setStorageSync('userlist',result.data.user)
						}
					}
				})
			},
			getRequestParams() {
				let url = location.href;
				let requestParams = {};
				if (url.indexOf("?") !== -1) {
					let str = url.substr(url.indexOf("?") + 1); //截取?后面的内容作为字符串
					console.log(str,'?后面的内容');
					let strs = str.split("&"); //将字符串内容以&分隔为一个数组
					console.log(strs,'以&切割的数组');
					for (let i = 0; i < strs.length; i++) {
						requestParams[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
						// 将数组元素中'='左边的内容作为对象的属性名，'='右边的内容作为对象对应属性的属性值
					}
				}
				console.log(requestParams, "处理后的对象");
				return requestParams;
			},
			directRightUrl() {
			    let { href, protocol, host, pathname, search, hash } = window.location
			    search = search || "?"
			    let newHref = `${protocol}//${host}${pathname}${search}${hash}`
				console.log("newHref=",newHref);
			    if (newHref !== href) {
					window.location.replace(newHref)
				}
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uview-ui/index.scss';
	/* 自定义公共css */
	@import '@/common/common.scss';
</style>